//
//  PictureBrowseViewController.m
//  IYTLCProduct
//
//  Created by administrator on 2017/4/5.
//  Copyright © 2017年 hs. All rights reserved.
//

#import "PictureBrowseViewController.h"
#import "UIColor+ColorUtility.h"
#import "PublicConfig.h"

@interface PictureBrowseViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *viewArray;
@end

@implementation PictureBrowseViewController

#pragma mark - Getters
- (NSMutableArray *)viewArray
{
    if (_viewArray == nil) _viewArray = [[NSMutableArray alloc] init];
    return _viewArray;
}

#pragma mark - DrawUI
- (void)setupNavBar
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 50, 25);
    [btn setTitle:@"删除" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn addTarget:self action:@selector(clickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    UIView *messageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
    messageView.bounds = CGRectOffset(messageView.bounds, -15, 0);
    [messageView addSubview:btn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:messageView];
}

- (void)setContentView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*self.imageArray.count, SCREEN_HEIGHT);
    for (int i = 0; i < self.imageArray.count; ++i)
    {
        PictureView *pic = [[PictureView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        pic.par = self;
        pic.imageView.image = self.imageArray[i];
        [self.scrollView addSubview:pic];
        [self.viewArray addObject:pic];
    }
    [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH*_currentIndex, 0)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = NO;
    if (_backRefreshAction) _backRefreshAction();
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupNavBar];
    [self setContentView];
    [self updateTitle];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Interact
- (void)singleTapOnPictureView
{
    BOOL barHidden = self.navigationController.navigationBarHidden;
    [self.navigationController setNavigationBarHidden:!barHidden animated:YES];
}

- (void)updateTitle
{
    self.title = [NSString stringWithFormat:@"%d/%d", _currentIndex+1, (int)self.viewArray.count];
}

- (void)clickDeleteButton
{
    if (_currentIndex == self.viewArray.count-1)
    {
        [self.viewArray[_currentIndex] removeFromSuperview];
        [self.viewArray removeObjectAtIndex:_currentIndex];
        [self.imageArray removeObjectAtIndex:_currentIndex];
        if (self.viewArray.count)
        {
            --_currentIndex;
            [self.scrollView setContentOffset:CGPointMake(SCREEN_WIDTH*_currentIndex, 0) animated:NO];
            self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*self.viewArray.count, SCREEN_HEIGHT);
            [self updateTitle];
        }
        else
            [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self.viewArray[_currentIndex] removeFromSuperview];
        [self.viewArray removeObjectAtIndex:_currentIndex];
        [self.imageArray removeObjectAtIndex:_currentIndex];
        for (int i = _currentIndex; i < self.viewArray.count; ++i)
        {
            UIView *view = self.viewArray[i];
            view.frame = CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        }
        self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*self.viewArray.count, SCREEN_HEIGHT);
        [self updateTitle];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _currentIndex = (int)(scrollView.contentOffset.x/SCREEN_WIDTH);
    [self updateTitle];
}

@end



@implementation PictureView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor blackColor];
        self.minimumZoomScale = 1;
        self.maximumZoomScale = 2.5;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.delegate = self;
        
        self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.imageView];
        self.imageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
        [self.imageView addGestureRecognizer:singleTap];
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTap.numberOfTapsRequired = 2;
        [self.imageView addGestureRecognizer:doubleTap];
        for (UITapGestureRecognizer *tap in self.imageView.gestureRecognizers)
            if ([tap isKindOfClass:[UITapGestureRecognizer class]])
                [tap requireGestureRecognizerToFail:doubleTap];
    }
    return self;
}

- (BOOL)isZoomed
{
    return self.zoomScale != self.minimumZoomScale;
}

- (CGRect)zoomRectForScale:(CGFloat)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.frame.size.height / scale;
    zoomRect.size.width  = self.frame.size.width  / scale;
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}

- (void)zoomToLocation:(CGPoint)location
{
    CGFloat newScale;
    CGRect zoomRect;
    if ([self isZoomed])
        zoomRect = self.bounds;
    else
    {
        newScale = self.maximumZoomScale;
        zoomRect = [self zoomRectForScale:newScale withCenter:location];
    }
    [self zoomToRect:zoomRect animated:YES];
}

- (void)singleTap
{
    if ([self.par respondsToSelector:@selector(singleTapOnPictureView)]) [self.par singleTapOnPictureView];
}

- (void)doubleTap:(UITapGestureRecognizer *)tap
{
    [self zoomToLocation:[tap locationInView:self]];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

@end
